 // var baseUrl = 'https://wx-api.freshlogic.cn'; //https://www.freshlogic.cn
//密钥  abd0965334b6eb69068fd1724bfd1e29
       //var baseUrl = 'https://wx-qql-api.freshlogic.cn';//生产
          var baseUrl = "https://api-wechart.howsale.tech"
        // var baseUrl = "http://192.168.3.111:8870"
         
 const GET = 'GET';
 const POST = 'POST';
 function request(method, url, data, isForm, isLoad) {
     return new Promise(function(resolve, reject) {
         var token = wx.getStorageSync('token');    
         var header = {};
         if (isForm) 
{             header = {
                 "content-Type": "application/x-www-form-urlencoded",
             }
         }else {
             header = {
                 "content-type": "application/json;charset=UTF-8",         
             }
             if (url == "/app/or/getWxOrder") {
                 url = "/app/or/getWxOrder/" + data;
              }
         }        
         if(url == "/app/goodscollectiondtl/delete"){
                 url ='/app/goodscollectiondtl/delete/'+ data;
         } else if (url == "/app/ck/recipientinfo/queryByGroupNo") {
             url = "/app/ck/recipientinfo/queryByGroupNo/" + data;
         } else if (url == "/app/ck/recipientinfo/deleteById/") {
             url = "/app/ck/recipientinfo/deleteById/" + data    
         }else if(url == '/app/co/goodsevaluateinfo/info'){
              url = '/app/co/goodsevaluateinfo/info/' + data 
         }else if(url == '/app/mobilerel/getPhoneCaptcha'){
             url = '/app/mobilerel/getPhoneCaptcha/'+ data
         }else if(url == '/app/wx/bindInvitationCode'){
             url = '/app/wx/bindInvitationCode/'+data
         }else if(url == '/app/ck/invoiceinfo'){
             url = '/app/ck/invoiceinfo/info/'+data
         }else if(url == '/app/blessinggavedtl/seeBlessing'){
             url = '/app/blessinggavedtl/seeBlessing/'+data
         }else if(url == '/app/blessinginfo/delete'){
             url = '/app/blessinginfo/delete/'+data
         }else if(url == '/app/ck/getCkInfo'){
             url = '/app/ck/getCkInfo?ckNo=' + data
         }else if(url == '/app/goods/getSkGoodsInfo'){
             url = '/app/goods/getSkGoodsInfo/' + data
         }else if(url == '/app/co/goodsevaluateinfo/list'){
             url == '/app/co/goodsevaluateinfo/list?params='+data
         }
         
         if (token) {
             header['token'] = token
         }
                header['coNo'] = 'CO1608195431952541'; //测试
                   // header['coNo'] = 'CO1608195431952541'; //生产
                   // header['coNo'] = 'CO2021011211950932076032'; //新测试
                     
           wx.showLoading();
         
         wx.request({
             url: baseUrl + url,
             method: method,
             data: method === POST && isForm === false ? JSON.stringify(data) : data,
             header: header,
             success(res) { 
                  wx.hideLoading();                                   
                 if (isForm === true) {
                     if(res.data.code == 0){
                         resolve(res.data);
                     }else{             
                             wx.showToast({
                                title:res.data.msg,
                                icon: 'none'
                             });   
                     }          
                 } else {
                     if (res.data.code == 0) {
                         resolve(res.data);
                     } else {
                         if(url == '/app/ck/recipientinfo/queryByGroup' || url == '/app/blessinggavedtl/queryNewBlessing'|| url=='/app/goodscollectiondtl/save'){
                             resolve(res.data);
                             
                         }else if(url == '/app/egc/ckgiftcardinfo/saveCkGiftCardReceive'){
                             wx.showToast({
                                title:res.data.msg,
                                icon: 'none'
                             });
                             resolve(res.data);
                         }else{
                             
                         wx.showToast({
                            title:res.data.msg,
                            icon: 'none'
                         });
                         }
                         
                     }
                 }
             },
             fail(err) {
                 reject(err)
                
               // if (url == "/app/wx/login") {
                     wx.hideLoading();
                 // }

             }
         })
     })
 }
 exports.request = request;
function requestFile(url, file, data) {
    
    return new Promise(function(resolve, reject) {
        var token = wx.getStorageSync('token');
        var header = {
            "content-Type": "multipart/form-data;charset=utf-8"
        }
        if (token) {
            header['token'] = token
        }
        wx.showLoading();
     const hah =  wx.uploadFile({
            url: baseUrl + url,
            filePath: file,
            name: 'file',
            header: header,
            success(res) {
               wx.hideLoading();
                if (res.data.code == 0) {
                    resolve(res.data);
                } else {
                    if (res.data.code == 403) {
                        resolve(res.data);
                    }
                    resolve(res.data);
                }
            },
            fail(err) {
                reject(err)
                wx.hideLoading();
            }
        })
     hah.onProgressUpdate((res) => {
                      const uploadProgress = res.progress;    
                      console.log('上传进度', res.progress);
                      console.log('已经上传的数据长度', res.totalBytesSent);
                      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
                  });
    })
}
exports.requestFile = requestFile;